﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using PetaPoco;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.DbService
{
    internal class SqlSugarService : ISqlSugar
    {
        private readonly IConfiguration configuration;
        private readonly ILogger<PetaPocoService> logger;
        private readonly IWebHostEnvironment webHost;
        private readonly ConnectionConfig config;

        public SqlSugarService(IConfiguration configuration, ILogger<PetaPocoService> logger, IWebHostEnvironment webHost)
        {
            this.configuration = configuration;
            this.webHost = webHost;

            config = new ConnectionConfig()
            {
                // 数据库类型，可以是 SqlServer、MySql、Sqlite 等
                DbType = DbType.SqlServer, // 使用 SQL Server
                ConnectionString = configuration.GetConnectionString("BaBizDb"), // 连接字符串
                IsAutoCloseConnection = true // 自动关闭连接
            };
        }

        public SqlSugarClient Create(bool keepAlive = false) => new SqlSugarClient(config);

        public SqlSugarClient Create(string connName, bool keepAlive = false)
        {
            return new SqlSugarClient(config);
        }
    }
}
